package login;

import Entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        response.setContentType("text/html;charset=utf-8");

        if (username.equals("123") && password.equals("123")) {
            response.getWriter().write(
                    "<script language='javascript'>" +
                            "alert('登录成功');" +
                            "window.location.href='./main.jsp';" +
                            "</script>");
            User user = new User(username, password);
            request.getSession().setAttribute("User", user);
        } else {
            response.getWriter().write(
                    "<script language='javascript'>" +
                            "alert('用户名或密码错误');" +
                            "window.location.href='./index.jsp'" +
                            "</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
